import time
from io import BytesIO
import numpy as np

from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image

driver = webdriver.Chrome()

# 图片爬取
driver.get("https://blog.51cto.com/hanzhichao/3211285")
driver.fullscreen_window()
time.sleep(2)
width = driver.execute_script("return document.documentElement.scrollWidth")

page_height = driver.execute_script("return document.documentElement.scrollHeight")
window_height = driver.get_window_size()['height']  # 窗口高度
driver.save_screenshot('qq.png')

if page_height > window_height:
    # 需要滚动的次数
    n = page_height // window_height
    # 打开截图并转为二维矩阵
    base_mat = np.atleast_2d(Image.open('qq.png'))

    for i in range(n):
        driver.execute_script(f'document.documentElement.scrollTop={window_height * (i + 1)};')
        time.sleep(.5)
        # 保存截图
        driver.save_screenshot(f'qq_{i}.png')
        # 打开截图并转为二维矩阵
        mat = np.atleast_2d(Image.open(f'qq_{i}.png'))
        # 拼接图片的二维矩阵
        base_mat = np.append(base_mat, mat, axis=0)
    Image.fromarray(base_mat).save('hao123.png')

# driver.set_window_size(width, height)
# time.sleep(1)
# driver.get_screenshot_as_file('11.png')

driver.quit()


driver.quit()
